package cn.exrick.xboot.modules.content.serviceimpl;

import cn.exrick.xboot.common.utils.BeanConvertUtils;
import cn.exrick.xboot.common.utils.SnowFlakeUtil;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.content.dao.HontPostDao;
import cn.exrick.xboot.modules.content.entity.HontDepartmentPost;
import cn.exrick.xboot.modules.content.entity.HontPost;
import cn.exrick.xboot.modules.content.entity.HontVo.HontPostVo;
import cn.exrick.xboot.modules.content.service.HontPostService;
import cn.exrick.xboot.modules.content.service.mybatis.IHontDepartmentPostService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * 岗位接口实现
 * @author hml
 */
@Slf4j
@Service
@Transactional
public class HontPostServiceImpl implements HontPostService {

    @Autowired
    private HontPostDao hontPostDao;
    @Autowired
    private IHontDepartmentPostService iHontDepartmentPostService;

    @Override
    public HontPostDao getRepository() {
        return hontPostDao;
    }

    @Override
    public Page<HontPost> findByCondition(HontPost hontPost, SearchVo searchVo, Pageable pageable) {

        return hontPostDao.findAll(new Specification<HontPost>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<HontPost> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                Path<String> titleField = root.get("title");

                List<Predicate> list = new ArrayList<Predicate>();
                if(StrUtil.isNotBlank(hontPost.getTitle())){
                    list.add(cb.like(titleField,'%'+hontPost.getTitle().trim()+'%'));
                }

                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public Optional<HontPost> findById(String postId) {
        return hontPostDao.findById(postId);
    }

    @Override
    public List<HontPost> findAll() {
        return hontPostDao.findAll();
    }

    @Override
    public HontPost findByFormId(String formId) {
        return hontPostDao.findByFormId(formId);
    }

    @Override
    public HontPost findByTitle(String formTitle) {
        return hontPostDao.findByTitle(formTitle);
    }

    @Override
    public HontPost addHontPost(HontPostVo hontPostVo) {
        HontPost hontPost = BeanConvertUtils.copy(hontPostVo, HontPost.class);
        String id=String.valueOf( SnowFlakeUtil.getFlowIdInstance().nextId());
        hontPostDao.save(hontPost);
        List<HontDepartmentPost> hontDepartmentPosts = hontPostVo.getHontDepartmentPosts();
        hontDepartmentPosts.stream().forEach(t->t.setHontPostId(id));
        iHontDepartmentPostService.addBatchHontDepartmentPost(hontDepartmentPosts);
        return hontPost;
    }

    @Override
    public HontPost updateHontPost(HontPostVo hontPostVo) {
        HontPost hontPost = hontPostDao.findById(hontPostVo.getId()).get();
        if (hontPost != null){
            Date createTime = hontPost.getCreateTime();
            hontPost.setCreateTime(createTime);
            hontPost.setTitle(hontPostVo.getTitle());
            hontPost.setFormId(hontPostVo.getFormId());
        }
        hontPostDao.saveAndFlush(hontPost);
        List<HontDepartmentPost> hontDepartmentPosts = hontPostVo.getHontDepartmentPosts();
        hontDepartmentPosts.stream().forEach(t->t.setHontPostId(hontPostVo.getId()));
        iHontDepartmentPostService.updateBatchHontDepartmentPost(hontDepartmentPosts, hontPostVo.getId());
        return hontPost;
    }

    /*@Override
    public List<HontPost> findAllByTitle(List<String> postTitles) {
        return hontPostDao.findAllByTitle(postTitles);
    }*/
}